{% extends 'base_backend.html' %}
{% load static %}
{% load my_filter %}

{% block header_tail %}
    <link rel="stylesheet" href="{% static 'zhongbao/zhongbao_admin/css/style.css' %}">
    <link rel="stylesheet" href="{% static 'zhongbao/zhongbao_admin/css/iconfont.css' %}">
    <link rel="stylesheet" href="{% static 'plugins/datatables/dataTables.bootstrap.css' %}">
    <link rel="stylesheet" href="{% static 'public/ComboSelect/css/combo.select.css' %}">
    <link rel="stylesheet" href="{% static 'zhongbao/config/css/main.css' %}">
    <link rel="stylesheet" href="{% static 'laydate/theme/default/laydate.css' %}">
    <script src="{% static 'laydate/laydate.js' %}"></script>
    <script src="{% static 'plugins\jQuery\jquery-2.2.3.min.js' %}"></script>

    <script src="{% static 'plugins/vue1/vue.js' %}"></script>

{% endblock header_tail %}

{% block section_content %}

    <div>
        <div class="box-header with-border">
            <h3 class="box-title">
                误报审核
            </h3>
        </div>
        <!-- form start -->
        <div class="box box-primary">
            <div class="box-header with-border" style="border-color: #1b6d85">
                <!-- Date range -->
                <form role="form" id="searchForm" method="get" action="">
                    {% csrf_token %}
                    <div style="width: 95%; float: left;">
                        <div style="float: left;" class="input-group select-config-div">
                            <label for="taskId" class="control-label">任务ID：</label>
                            <p>
                                <input id="taskId" name="task_id" type="text" class="config-input"
                                       value="{{ task_id }}">
                            </p>
                        </div>
                        <div style="float: left;" class="input-group select-config-div">
                            <label for="project_id" class="control-label">项目ID：</label>
                            <p>
                                <input id="project_id" name="project_id" type="text" class="config-input"
                                       value="{{ project_id }}">
                            </p>
                        </div>
                        <div style="float: left;" class="input-group select-config-div">
                            <label for="id" class="control-label">任务状态：</label>
                            <select name="status_id" id="taskStatus" class="config-input">
                                {% for status in status_list %}
                                    {% if status.id == status_id %}
                                        <option selected="selected"
                                                value="{{ status.id }}">{{ status.description }}</option>
                                    {% else %}
                                        <option value="{{ status.id }}">{{ status.description }}</option>
                                    {% endif %}
                                {% endfor %}
                            </select>
                        </div>
                        {% if author == 'admin' or author == 'agent' %}
                            <div style="float: left;" class="input-group select-config-div">
                                <label for="username" class="control-label">用户名：</label>
                                <input id="username" type="text" name="username" class="config-input"
                                       value="{{ username }}">
                            </div>
                        {% endif %}

                        {% if author == 'admin' %}
                            <div style="float: left;" class="input-group select-config-div">
                                <label for="agent_name" class="control-label">代理名称：</label>
                                <select name="agent_name" id="agent_name" class="config-input">
                                    <option value="">--------</option>
                                    {% for agent in agents %}
                                        {% if agent.id == agent_id %}
                                            <option selected="selected"
                                                    value="{{ agent.id }}">{{ agent.agent_name }}</option>
                                        {% else %}
                                            <option value="{{ agent.id }}">{{ agent.agent_name }}</option>
                                        {% endif %}
                                    {% endfor %}
                                </select>
                            </div>
                        {% endif %}
                        <div style="float: left;" class="input-group select-config-div">
                            <label for="distort_status" class="control-label">审核状态：</label>
                            <select name="distort_status" id="distort_status" class="config-input">
                                {% for distort in distort_status_list %}
                                    {% if distort.distort_id == distort_status %}
                                        <option selected="selected"
                                                value="{{ distort.distort_id }}">{{ distort.distort_status }}</option>
                                    {% else %}
                                        <option value="{{ distort.distort_id }}">{{ distort.distort_status }}</option>
                                    {% endif %}
                                {% endfor %}
                            </select>
                        </div>
                        <div style="float: left;" class="input-group select-config-div">
                            <label for="id" class="control-label">每页显示：</label>
                            <select name="limit" id="showNumSelect" class="config-input">
                                {% for i in limits %}
                                    {% if i == limit %}
                                        <option selected="selected" value="{{ i }}">{{ i }}条</option>
                                    {% else %}
                                        <option value="{{ i }}">{{ i }}条</option>
                                    {% endif %}
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="input-group" style="width: 5%; float: right; margin-top: 70px;">
                        <input type="submit" class="form-control pull-left btn btn-sm btn-primary" value="检索">
                    </div>
                </form>
            </div>
            <!-- 展示搜索出来的任务 -->
            <div id="main">
                <div class="box-body">
                    <form id="model_form" role="form" method="post">
                        {% csrf_token %}
                        <div class="group">
                            <table id="task_each_table" border="1"
                                   class="table table-bordered table-hover table-striped">
                                <thead>
                                <tr>
                                    <th>
                                    <span style="float: left">
                                        任务ID
                                    </span>
                                    </th>
                                    <th>
                                    <span style="float: left">
                                        项目ID
                                    </span>
                                    </th>
                                    <th>
                                    <span style="float: left">
                                        网站名称
                                    </span>
                                    </th>
                                    <th>
                                    <span style="float: left">
                                        网站链接
                                    </span>
                                    </th>
                                    <th>
                                    <span style="float: left">
                                        提交误报时间
                                    </span>
                                    </th>
                                    <th>
                                    <span style="float: left">
                                        错误信息
                                    </span>
                                    </th>
                                    <th>
                                    <span style="float: left">
                                        误报审核状态
                                    </span>
                                    </th>
                                    <th>
                                    <span style="float: left">
                                        任务状态
                                    </span>
                                    </th>
                                    <th>
                                    <span style="float: left">
                                        驳回误审原因
                                    </span>
                                    </th>
                                    {% if author == 'admin' %}
                                        <th>
                                    <span style="float: left">
                                        代理名称
                                    </span>
                                        </th>
                                    {% endif %}
                                    {% if author == 'admin' or author == 'agent' %}
                                        <th>
                                    <span style="float: left">
                                        用户名
                                    </span>
                                        </th>
                                        <th>
                                    <span style="float: left">
                                        姓名
                                    </span>
                                        </th>
                                        <th>
                                    <span style="float: left">
                                        申请误审原因
                                    </span>
                                        </th>

                                        <th>
                                    <span style="float: left">
                                        操作
                                    </span>
                                        </th>
                                    {% endif %}
                                </tr>
                                </thead>
                                <tbody>
                                {% for task in tasks %}
                                    <tr>
                                        <td>{{ task.id }}</td>
                                        <td>{{ task.project_id }}</td>
                                        <td title="{{ task.webname }}">{{ task.site_name|truncatechars_html:15 }}</td>
                                        <td><a href="{{ task.site_url }}" title="{{ task.site_url }}">
                                            {{ task.site_url|truncatechars_html:30 }}</a>
                                        </td>
                                        <td> {{ task.distort_time|timestamp_format:'%Y-%m-%d %H:%M:%S' }}</td>
                                        <td>{{ task.disqualify_reason }}</td>
                                        <td>{{ task.is_distort|distort_status }}</td>
                                        <td>{{ task.status.description }}</td>
                                        <td>{{ task.reject_mistrial_reason }}</td>
                                        {% if author == 'admin' %}
                                            <td>{{ task.agency.agent_name }}</td>
                                        {% endif %}
                                        {% if author == 'admin' or author == 'agent' %}
                                            <td title="{{ task.user.username }}">{{ task.user.username }}</td>
                                            <td title="{{ task.user.real_name }}">{{ task.user.real_name }}</td>
                                            <td>{{ task.apply_mistrial_reason }}</td>

                                            <td>
                                                {% if task.is_distort == 1 and task.status.code == 5 %}
                                                    <a class="btn btn-info btn-xs"
                                                       onclick="distortActionFail('{{ task.id }}')">
                                                        误报通过
                                                    </a>

                                                    <a class="btn btn-info btn-xs"
                                                       onclick="alarms_fail('{{ task.id }}')">
                                                        误报不通过
                                                    </a>
                                                    <a class="btn btn-info btn-xs"
                                                       onclick="distorRetrieve('{{ task.id }}', false)">
                                                        回收
                                                    </a>
                                                {% endif %}
                                                <a href="{% url 'maintain:task_detail_agent' %}?task_id={{ task.id }}"
                                                   class="btn btn-info btn-xs">详情</a>
                                            </td>
                                        {% endif %}
                                    </tr>
                                {% endfor %}
                                </tbody>
                            </table>
                            <p class="" id="paginator">
                                <a class="btn btn-default btn-xs" onclick="toPage('home')">首页</a>
                                <a class="btn btn-default btn-xs" onclick="toPage('last')">上一页</a>
                                <a class="btn btn-default btn-xs" onclick="toPage('next')">下一页</a>
                                <input type="text" id="page-judge" style="width: 30px;height:20px; margin-left: 20px"
                                       onkeyup="toPage('judge')">
                                <a class="btn btn-default btn-xs" onclick="toPage('next')">尾页</a>
                                <a class="btn btn-default btn-xs" onclick="toPage('judge')">跳页</a>
                                <span v-show="total">共{{ count }}个</span>
                            </p>
                        </div>
                        <!-- /.box-footer -->
                        <div class="box-footer">
                            <button class="btn btn-primary no-display" id="submit_button">确认</button>
                            <button type="button" class="btn btn-primary" id="save_submit" name="_save"
                                    style="display: none">sure
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="viewAllMessage" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         style="top:40%">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div id="viewAllBoxMsg" class="modal-body" style="overflow:auto;text-align:center;">
                </div>
                <div class="modal-footer" style="text-align:center;">
                    <button type="button" class="btn btn-default"
                            data-dismiss="modal">取消
                    </button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="operationMessage1" tabindex="-1" role="dialog"
         aria-labelledby="myModalLabel" style="top:40%">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header" style="background-color: #307095;font-weight: bold;">
                    <label>请填写不通过原因：</label>
                </div>

                <div id="msgBoxMsg" class="modal-body" style="overflow:auto;text-align:center;">

                    <input style="width:400px;height:100px;" type="text" id="fankui1" name="fankuiyy"/>

                </div>
                <div class="modal-footer" style="text-align:center;">
                    <button type="button" class="btn btn-default" onclick="distortActionNodePass()"
                            data-dismiss="modal">确认
                    </button>

                    <button type="button" class="btn btn-default"
                            data-dismiss="modal">取消
                    </button>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="operationMessage2" tabindex="-1" role="dialog"
         aria-labelledby="myModalLabel" style="top:40%">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header" style="background-color: #307095;font-weight: bold;">
                    <label>请填写通过原因：</label>
                </div>

                <div id="msgBoxMsg" class="modal-body" style="overflow:auto;text-align:center;">

                    <input style="width:400px;height:100px;" type="text" id="fankui2" name="fankuiyy"/>

                </div>
                <div class="modal-footer" style="text-align:center;">
                    <button type="button" class="btn btn-default" onclick="distortAction()"
                            data-dismiss="modal">确认
                    </button>

                    <button type="button" class="btn btn-default"
                            data-dismiss="modal">取消
                    </button>
                </div>
            </div>
        </div>
    </div>
    <input id="taskIds" value="" style="display:none;"></input>
    <script>
        function changeColor(id, color) {
            //$(id).parent().parent().parent().children().children().children().find("li[color='green']");
            $(id).parent().parent().addClass("active");
            $(id).children().css("color", color);
        }


        changeColor('#maintain_task_distort_audit', '#00FF7F');

        // 查看全部
        function viewAll(distort_cvalue) {
            $('#viewAllBoxMsg').text(distort_cvalue);
            $('#viewAllMessage').modal();
        }

        // 合格api
        function distortPass(task_id) {
            $.ajax({
                url: "{% url 'maintain:qualified' %}",
                type: "POST",
                dataType: "json",
                data: {'csrfmiddlewaretoken': '{{ csrf_token }}', 'task_id': task_id},
                success: function (resp) {
                    alert(resp.message);
                    window.location.reload();
                },
                error: function (xhr, status, p3, p4) {
                    var err = "Error " + " " + status + " " + p3;
                    if (xhr.responseText && xhr.responseText[0] == "{")
                        err = JSON.parse(xhr.responseText).message;
                    alert(err + '合格状态修改失败！！！请点击重新修改');
                }
            });
        }

        // 通过api
        function distortAdopt(task_id) {
            $.ajax({
                url: "{% url 'task:adopt' %}",
                type: "POST",
                dataType: "json",
                data: {'csrfmiddlewaretoken': '{{ csrf_token }}', 'tids': task_id},
                success: function (resp) {
                    alert(resp.message);
                    window.location.reload();
                },
                error: function (xhr, status, p3, p4) {
                    var err = "Error " + " " + status + " " + p3;
                    if (xhr.responseText && xhr.responseText[0] == "{")
                        err = JSON.parse(xhr.responseText).message;
                    alert(err + '通过状态修改失败！！！请点击重新修改');
                }
            });
        }

        // 回收
        function distorRetrieve(tids, addNewOne) {
            $.ajax({
                url: "{% url 'task:retrieve' %}",
                type: "POST",
                dataType: "json",
                data: {'csrfmiddlewaretoken': '{{ csrf_token }}', 'tids': tids, 'addNewOne': addNewOne},
                success: function (resp) {
                    alert(resp.message);
                    window.location.reload();
                },
                error: function (xhr, status, p3, p4) {
                    var err = "Error " + " " + status + " " + p3;
                    if (xhr.responseText && xhr.responseText[0] == "{")
                        err = JSON.parse(xhr.responseText).message;
                    alert(err + '通过状态修改失败！！！请点击重新修改');
                }
            });
        }

        // 误审通过
        function distortAction(task_id) {
            var tids = $("#taskIds").val();
            var fankui = $("#fankui2").val();

            $.post({
                url: "{% url 'maintain:distort_audit_pass' %}",
                data: {
                    csrfmiddlewaretoken: "{{ csrf_token }}",
                    task_id: tids,
                    reason: fankui
                },
                success: function (resp) {
                    var result = resp.message;
                    if (result === "误审通过成功") {
                        distortPass(tids);
                    } else {
                        alert(result)
                    }
                },
                error: function () {
                    alert('误审标记失败')
                }
            });
        }

        // 误审不通过
        function distortActionNodePass() {
            var tids = $("#taskIds").val();
            var fankui = $("#fankui1").val();
            $.post({
                url: "{% url 'maintain:distort_audit_node_pass' %}",
                data: {
                    csrfmiddlewaretoken: "{{ csrf_token }}",
                    task_id: tids,
                    reason: fankui
                },
                success: function (resp) {
                    var result = resp.message;
                    alert(result)
                },
                error: function () {
                    alert('误审标记失败')
                }
            });
        }

        // 获取当前url参数值
        function GetQueryValue1(queryName) {
            var reg = new RegExp("(^|&)" + queryName + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            } else {
                return null;
            }
        }

        // 翻页
        function toPage(type) {
            location_url = window.location.href;
            var page_num = GetQueryValue1('page');
            if (!page_num) {
                page_num = 1
            }
            if (type === 'last') {
                if (Number(page_num) > 1) {
                    page_num = Number(page_num) - 1
                } else {
                    page_num = 1
                }
            } else if (type === 'next') {
                page_num = Number(page_num) + 1
            } else if (type === 'judge') {
                page_num = $("#page-judge").val()
            } else if (type === 'home') {
                page_num = 1
            } else {
                alert('参数错误');
                return false;
            }
            var page = 'page=' + page_num;
            if (location_url.indexOf('page') !== -1) {
                window.location.href = location_url.replace(/page=\d+/, page)
            } else {
                if (location_url.indexOf('?') === -1) {
                    window.location.href = location_url + '?' + page
                } else {
                    window.location.href = location_url + '&' + page;
                }
            }
        }

        function alarms_fail(tids) {
            if (tids) {
                $("#taskIds").val(tids);
                $("#operationMessage1").modal();
            } else {
                return false
            }
        }

        function distortActionFail(tids) {
            if (tids) {
                $("#taskIds").val(tids);
                $("#operationMessage2").modal();
            } else {
                return false
            }
        }
    </script>
{% endblock section_content %}
